package com.zhuchang.self_rpc.test.democoding;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

/**
 * @Author RenPu
 * @Date 2022/2/10 12:09
 * @Version 1.0
 * @Description:
 **/
public class UnpoolByteBuffer01 {

    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.copiedBuffer("Hello World ....******", CharsetUtil.UTF_8);

        if(byteBuf.hasArray()){
            byte[] content = byteBuf.array();
            System.out.println(new String(content,CharsetUtil.UTF_8));

            System.out.println(byteBuf.arrayOffset()); // 0
            System.out.println(byteBuf.readerIndex()); // 0
            System.out.println(byteBuf.writerIndex()); // 12
            System.out.println(byteBuf.capacity()); // 36

            //System.out.println(byteBuf.readByte()); //
            System.out.println(byteBuf.getByte(0)); // 104

            int len = byteBuf.readableBytes(); //可读的字节数  12
            System.out.println("len=" + len);

            //使用for取出各个字节
            for (int i = 0; i < len; i++) {
                System.out.println((char) byteBuf.getByte(i));
            }

            //按照某个范围读取
            System.out.println(byteBuf.getCharSequence(0, 4, Charset.forName("utf-8")));
            System.out.println(byteBuf.getCharSequence(4, 6, Charset.forName("utf-8")));
        }
    }








        }

